﻿Public Class frmCauThu

    Dim lt As New dsCauThu
    Dim sa As New dsSoAo
    Dim context As New QLDBDataContext
    Dim hinh As New LayHinh
    Dim vt As New ViTri
    Dim vt1 As New ViTri
    Dim qr As IQueryable(Of dsCauThu)

    Const PagesSize As Integer = 10
    Dim curPage As Integer
    Dim numPages As Integer

    Dim curSearchPage As Integer
    Dim numSearchPages As Integer
    Dim tk As Boolean = False

    Sub SetNavigationState()
        btnToi.Enabled = (curPage < numPages)
        btnCuoi.Enabled = (curPage < numPages)
        btnLui.Enabled = (curPage > 1)
        btnDau.Enabled = (curPage > 1)
    End Sub

    Sub SetNavigationStateSearch()
        btnToi.Enabled = (curSearchPage < numSearchPages)
        btnCuoi.Enabled = (curSearchPage < numSearchPages)
        btnLui.Enabled = (curSearchPage > 1)
        btnDau.Enabled = (curSearchPage > 1)
    End Sub

    Private Sub SelectedCauThu()
        Dim ct As dsCauThu = gridCauThu.CurrentRow.DataBoundItem()
        Dim _ct = context.CAUTHUs.Single(Function(q) q.MaCT = ct.MaCT)

        txtTen.Text = ct.TenCT
        day.Value = _ct.NgaySinh
        cboQT.SelectedValue = _ct.MaQT
        picAvata.Image = hinh.ReDraw(ct.Avata, picAvata.Width, picAvata.Height)
        cboCLB.SelectedValue = _ct.MaCLB
    End Sub

    Private Sub gridCauThu_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles gridCauThu.CellContentClick
        Me.Cursor = Cursors.AppStarting
        SelectedCauThu()
    End Sub

    Private Sub DinhDangGrid()
        With gridCauThu
            .ColumnCount = 7
            .Columns(0).DataPropertyName = "MaCT"

            .Columns(1).DataPropertyName = "Avata"

            .Columns(2).DataPropertyName = "TenCT"

            .Columns(3).DataPropertyName = "ViTri"

            .Columns(4).DataPropertyName = "SoAo"

            .Columns(5).DataPropertyName = "TenDoi"

            .Columns(6).DataPropertyName = "QuocTich"

        End With
    End Sub

    Private Sub DanhSachDoi()
        Dim ds As New dsCLB
        Dim cbos As New dsCLB
        ds.LayDoiBongTK()
        cboCLB.DataSource = ds.LayDoiBongTK()
        cboCLB.DisplayMember = "TenCLB"
        cboCLB.ValueMember = "MaCLB"

        cboSDB.DataSource = cbos.LayDoiBongTK()
        cboSDB.DisplayMember = "TenCLB"
        cboSDB.ValueMember = "MaCLB"
    End Sub

    Private Sub DanhSachQT()
        Dim ltQT As New dsQuocGia
        Dim lts As New dsQuocGia

        cboQT.DataSource = ltQT.LayQuocTichTK()
        cboQT.DisplayMember = "TenQT"
        cboQT.ValueMember = "MaQT"

        cboSQG.DataSource = lts.LayQuocTichTK()
        cboSQG.DisplayMember = "TenQT"
        cboSQG.ValueMember = "MaQT"

    End Sub

    Private Sub LayDSVT()
        cboViTri.DataSource = vt.LayDanhSach()
        cboViTri.DisplayMember = "ViTri"
        cboViTri.ValueMember = "ViTri"

        cboSVT.DataSource = vt1.LayDanhSach()
        cboSVT.DisplayMember = "ViTri"
        cboSVT.ValueMember = "ViTri"
    End Sub

    Private Sub frmCauThu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Cursor = Cursors.AppStarting
        qr = lt.LayDSCauThu()
        numPages = Math.Ceiling(qr.Count() / PagesSize)
        curPage = 1
        lbPage.Text = String.Format("{0}/{1}", curPage, numPages)
        If cboCLB.DataSource Is Nothing Then
            DanhSachDoi()
        End If
        If cboSQG.DataSource Is Nothing Then
            DanhSachQT()
        End If
        If gridCauThu.DataSource Is Nothing Then
            DinhDangGrid()
        End If
        If cboViTri.DataSource Is Nothing Then
            LayDSVT()
        End If
        'If picAvata.Image Is Nothing Then
        '    picAvata.Image = hinh.ResizImg("images.jpg", picAvata.Width, picAvata.Height)
        'End If
        gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
        SetNavigationState()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Cursor = Cursors.AppStarting
        picAvata.Image = hinh.IMG
        txtTen.Text = String.Empty
        cboCLB.SelectedValue = -1
        cboQT.SelectedValue = -1
        day.Value = Date.Now()
        Me.Close()
        Me.Cursor = Cursors.Default
    End Sub



    Private Sub cboCLB_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboCLB.SelectedValueChanged
        Me.Cursor = Cursors.AppStarting
        Try
            cboSoAo.DataSource = sa.LaySoAo(cboCLB.SelectedValue)
            cboSoAo.DisplayMember = "TenSo"
            cboSoAo.ValueMember = "SoAo"
        Catch ex As Exception

        End Try
        Me.Cursor = Cursors.Default
    End Sub


    Private Sub CapNhat()
        Dim ct As dsCauThu = gridCauThu.CurrentRow.DataBoundItem()
        Dim _ct = context.CAUTHUs.Single(Function(q) q.MaCT = ct.MaCT)
        _ct.HoTen = txtTen.Text

        If _ct.MaCLB <> cboCLB.SelectedValue Then ' chuyển đến câu lạc bộ khác
            If cboCLB.SelectedValue = -1 Then 'thất nghiệp
                _ct.MaCLB = Nothing
                _ct.SoAo = Nothing

            Else 'có câu lạc bộ và số áo mới
                _ct.MaCLB = cboCLB.SelectedValue
                _ct.SoAo = cboSoAo.SelectedValue
                _ct.ViTri = cboViTri.SelectedValue
            End If
        Else

        End If
        If OpenFileDialog1.FileName <> "Chọn Ảnh Đại Diện Cho Cầu Thủ" Then 'đã chọn hình khác
            _ct.HINH = hinh.LuuHinh(OpenFileDialog1.FileName, "cauthu\")
        End If
        If day.Value <> _ct.NgaySinh Then
            _ct.NgaySinh = day.Value
        End If
        If cboQT.SelectedValue <> _ct.MaQT Then
            If cboQT.SelectedValue = -1 Then
                _ct.MaQT = Nothing
            Else
                _ct.MaQT = cboQT.SelectedValue
            End If
        End If
        context.SubmitChanges()
        gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)

        MessageBox.Show("Câp Nhật Thành Công")
    End Sub

    Private Sub Xoa()
        Dim ct As dsCauThu = gridCauThu.CurrentRow.DataBoundItem()
        Dim _ct = context.CAUTHUs.Single(Function(q) q.MaCT = ct.MaCT)
        context.CAUTHUs.DeleteOnSubmit(_ct)
        context.SubmitChanges()
        qr = lt.LayDSCauThu()
        SetDefault()
        numPages = Math.Ceiling(qr.Count() / PagesSize)
        curPage = 1
        gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
        SetNavigationState()
    End Sub

    Private Function laymax() As Integer
        Dim m = (From c In context.CAUTHUs Select c.MaCT).Max()
        Return m
    End Function

    Private Sub btnChinhSua_Click(sender As Object, e As EventArgs) Handles btnChinhSua.Click
        Me.Cursor = Cursors.AppStarting
        If Trim(txtTen.Text) = "" Then
            MessageBox.Show("Chưa Nhập Tên Cầu Thủ")
            txtTen.Focus()
            Return
        ElseIf IsNumeric(txtTen.Text) = True Then
            MessageBox.Show("Tên Cầu Thủ Không Phải Là Số")
            Return
        End If

        If picAvata.Image Is Nothing Then
            MessageBox.Show("Chưa Chọn Cầu Thủ")
            Return
        End If
        CapNhat()
        SetDefault()
        Me.Cursor = Cursors.Default
    End Sub

    Sub SetDefault()
        txtTen.Text = String.Empty
        txtTen.Focus()
        cboCLB.SelectedIndex = 0
        cboQT.SelectedIndex = 0
        day.Value = Date.Now()
        picAvata.Image = Nothing
    End Sub

    Private Sub ThemCauThu()
        Dim ct As New CAUTHU
        ct.HoTen = txtTen.Text
        ct.NgaySinh = day.Value
        ct.MaCT = laymax() + 1
        If cboCLB.SelectedValue <> -1 Then ' vào một câu lạc bộ còn không để trống
            ct.MaCLB = cboCLB.SelectedValue
            ct.SoAo = cboSoAo.SelectedValue
            ct.ViTri = cboViTri.SelectedValue
        End If
        If cboQT.SelectedValue <> -1 Then ' chọn luôn quốc tịch cho cầu thủ
            ct.MaQT = cboQT.SelectedValue
        End If

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 'chọn hình vì mới thêm mà
            picAvata.Image = hinh.ReDraw(Image.FromFile(OpenFileDialog1.FileName), picAvata.Width, picAvata.Height)
            ct.HINH = hinh.LuuHinh(OpenFileDialog1.FileName, "cauthu\")
        Else
            ct.HINH = "images.jpg"
        End If
        context.CAUTHUs.InsertOnSubmit(ct)
        context.SubmitChanges()
        gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
        SetDefault()
        MessageBox.Show("Thêm Cầu Thủ Thành Công")
    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        Me.Cursor = Cursors.AppStarting
        If Trim(txtTen.Text) = "" Then
            MessageBox.Show("Chưa Nhập Tên Cầu Thủ")
            txtTen.Focus()
            Return
        ElseIf IsNumeric(txtTen.Text) = True Then
            MessageBox.Show("Tên Cầu Thủ Không Phải Là Số")
            Return
        End If
        ThemCauThu()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub picAvata_Click(sender As Object, e As EventArgs) Handles picAvata.Click
        Me.Cursor = Cursors.AppStarting
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            picAvata.Image = hinh.ReDraw(Image.FromFile(OpenFileDialog1.FileName), picAvata.Width, picAvata.Height)
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnToi_Click(sender As Object, e As EventArgs) Handles btnToi.Click
        Me.Cursor = Cursors.AppStarting
        SetDefault()
        If tk = False Then
            curPage += 1

            lbPage.Text = String.Format("{0}/{1}", curPage, numPages)
            gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationState()
        Else
            curSearchPage += 1

            lbPage.Text = String.Format("{0}/{1}", curSearchPage, numSearchPages)
            gridCauThu.DataSource = qr.Skip((curSearchPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationStateSearch()
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnLui_Click(sender As Object, e As EventArgs) Handles btnLui.Click
        Me.Cursor = Cursors.AppStarting
        If tk = False Then
            curPage -= 1
            SetDefault()
            lbPage.Text = String.Format("{0}/{1}", curPage, numPages)
            gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationState()
        Else
            curSearchPage -= 1
            SetDefault()
            lbPage.Text = String.Format("{0}/{1}", curSearchPage, numSearchPages)
            gridCauThu.DataSource = qr.Skip((curSearchPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationStateSearch()
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnDau_Click(sender As Object, e As EventArgs) Handles btnDau.Click
        Me.Cursor = Cursors.AppStarting
        SetDefault()

        If tk = False Then 'đang ở trạng thái không tìm kiếm
            curPage = 1
            lbPage.Text = String.Format("{0}/{1}", curPage, numPages)
            gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationState()
        Else
            curSearchPage = 1
            lbPage.Text = String.Format("{0}/{1}", curSearchPage, numSearchPages)
            gridCauThu.DataSource = qr.Skip((curSearchPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationStateSearch()
        End If
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub btnCuoi_Click(sender As Object, e As EventArgs) Handles btnCuoi.Click
        Me.Cursor = Cursors.AppStarting
        SetDefault()
        If tk = False Then
            curPage = numPages
            lbPage.Text = String.Format("{0}/{1}", curPage, numPages)
            gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationState()
        Else
            curSearchPage = numSearchPages
            lbPage.Text = String.Format("{0}/{1}", curSearchPage, numSearchPages)
            gridCauThu.DataSource = qr.Skip((curSearchPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationStateSearch()
        End If
        Me.Cursor = Cursors.Default
    End Sub


    Private Sub btnLamLai_Click(sender As Object, e As EventArgs) Handles btnLamLai.Click
        Me.Cursor = Cursors.AppStarting
        If tk = True Then 'nếu đang ở trạng thái tìm kiếm đưa về trang chủ 
            qr = lt.LayDSCauThu()
            numPages = Math.Ceiling(qr.Count() / PagesSize)
            lbPage.Text = String.Format("{0}/{1}", curPage, numPages)
            curPage = 1
            gridCauThu.DataSource = qr.Skip((curPage - 1) * PagesSize).Take(PagesSize)
            SetNavigationState()
            tk = False
            cboSVT.SelectedIndex = 0
            txtTim.Text = String.Empty
            txtTim.Focus()
            cboSDB.SelectedIndex = 0
            cboSQG.SelectedIndex = 0
        Else 'còn nếu đang ở trạng thái trước tìm kiếm
            cboSVT.SelectedIndex = 0
            txtTim.Text = String.Empty
            txtTim.Focus()
            cboSDB.SelectedIndex = 0
            cboSQG.SelectedIndex = 0

        End If
        Me.Cursor = Cursors.Default
    End Sub


    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        Me.Cursor = Cursors.AppStarting
        If picAvata.Image Is Nothing Then
            MessageBox.Show("Chưa Chọn Cầu Thủ Cậu Ah!")
            Return
        End If
        If MessageBox.Show("Bạn Có Muốn Xóa Cầu Thủ Đã Chọn", "Yes Or No", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Xoa()
        Else
            SetDefault()
            Return
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnTimKiem_Click(sender As Object, e As EventArgs) Handles btnTimKiem.Click
        Me.Cursor = Cursors.AppStarting
        If Trim(txtTim.Text) = "" And cboSDB.SelectedIndex = 0 And cboSQG.SelectedIndex = 0 And cboSVT.SelectedIndex = 0 Then
            MessageBox.Show("Nhập Thông Tin Cần Tìm")
            Return
        Else
            qr = lt.BuilSearchQuery(txtTim.Text, cboSVT.SelectedValue, cboSQG.SelectedValue, cboSDB.SelectedValue)
            If qr.Count = 0 Then
                MessageBox.Show("Rất Tiếc Tìm Không Thấy")
            Else
                tk = True
                numSearchPages = Math.Ceiling(qr.Count() / PagesSize)
                curSearchPage = 1
                lbPage.Text = String.Format("{0}/{1}", curSearchPage, numSearchPages)

                gridCauThu.DataSource = qr.Skip((curSearchPage - 1) * PagesSize).Take(PagesSize)
                SetNavigationStateSearch()
            End If
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub gridCauThu_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles gridCauThu.CellContentDoubleClick
        Me.Cursor = Cursors.AppStarting
        SelectedCauThu()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub gridCauThu_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles gridCauThu.CellDoubleClick
        Me.Cursor = Cursors.AppStarting
        SelectedCauThu()
        Me.Cursor = Cursors.Default
    End Sub

End Class